package com.hang.controller;

import com.hang.common.Result;
import com.hang.exception.MyException;
import com.hang.model.dto.request.CommentModelDTO;
import com.hang.service.ICommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "评论管理接口")
@RequestMapping("comment")
@RestController
@Slf4j
public class CommentController {
    @Resource
    private ICommentService commentService;

    @ApiOperation(value = "新增评论接口")
    @PostMapping("add")
    public Result<Object> addComment(
            @RequestBody @Valid CommentModelDTO commentModelDTO
    ){
        try {
            commentService.createComment(commentModelDTO);
            return Result.ok().message("评论成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "删除评论接口")
    @DeleteMapping("delete")
    public Result<Object> deleteComment(
            @RequestParam("commentId") Long commentId,
            @RequestParam("userId") Long userId
    ){
        try {
            commentService.deleteComment(commentId,userId);
            return Result.ok().message("删除成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }
}
